/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.common;

import kz.kstu.ent.util.ColorUtil;
import kz.kstu.ent.util.DateUtil;
import kz.kstu.ent.util.NumberUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

import java.awt.*;
import java.util.Calendar;

/**
 * Date: 21.09.12
 * Time: 20:50
 *
 * @author Oleg Anastassov
 */
public final class ChartBuilder {

    private static final String AVERAGE_MARK = "Средний балл";


    private ChartBuilder() {
    }

    @Deprecated
    public static JFreeChart buildDemo() {
        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        ds.setValue(86.5, AVERAGE_MARK, DateUtil.formatDate(
                DateUtil.createDate(2012, Calendar.SEPTEMBER, 8), DateUtil.SIMPLE_DATE_PATTERN));
        ds.setValue(88.8, AVERAGE_MARK, DateUtil.formatDate(
                DateUtil.createDate(2012, Calendar.OCTOBER, 6), DateUtil.SIMPLE_DATE_PATTERN));
        ds.setValue(87.3, AVERAGE_MARK, DateUtil.formatDate(
                DateUtil.createDate(2012, Calendar.OCTOBER, 20), DateUtil.SIMPLE_DATE_PATTERN));
        ds.setValue(89.4, AVERAGE_MARK, DateUtil.formatDate(
                DateUtil.createDate(2012, Calendar.NOVEMBER, 4), DateUtil.SIMPLE_DATE_PATTERN));
        ds.setValue(93.7, AVERAGE_MARK, DateUtil.formatDate(
                DateUtil.createDate(2012, Calendar.NOVEMBER, 18), DateUtil.SIMPLE_DATE_PATTERN));
        ds.setValue(118.7, AVERAGE_MARK, DateUtil.formatDate(
                DateUtil.createDate(2012, Calendar.DECEMBER, 8), DateUtil.SIMPLE_DATE_PATTERN));

        JFreeChart chart = ChartFactory.createBarChart3D(
                "Средний балл",
                "Даты проведения тестирования",
                null,
                ds,
                PlotOrientation.HORIZONTAL,
                false,
                true,
                false
        );
        chart.setBackgroundPaint(ColorUtil.BACKGROUND_COLOR);

        CategoryPlot p = chart.getCategoryPlot();

        CategoryItemRenderer categoryItemRenderer = p.getRenderer();
        categoryItemRenderer.setBaseItemLabelGenerator(
                new StandardCategoryItemLabelGenerator("{2}", NumberUtil.DECIMAL_FORMAT)
        );
        categoryItemRenderer.setBaseItemLabelsVisible(true);

        BarRenderer renderer = (BarRenderer) p.getRenderer();
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setBarPainter(new StandardBarPainter());

        // use to print values inside the bars
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.CENTER,
                TextAnchor.CENTER)
        );

        // use to print values over the bars
//        renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition(
//                ItemLabelAnchor.CENTER,
//                TextAnchor.CENTER
//        ));

        return chart;
    }

}
